<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="票据单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNum">
              <a-input v-model="model.billNum" placeholder="请输入票据单号" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="radio" v-model="model.sex" dictCode="sex" placeholder="请选择性别" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idNum">
              <a-input v-model="model.idNum" placeholder="请输入身份证号" readOnly :disabled="disabled" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="balance">
              <a-input-number v-model="model.balance" placeholder="请输入余额" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="监室号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgCode">
              <j-select-depart v-model="model.orgCode" multi disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="入所时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="custodyDate">
              <j-date placeholder="请选择入所时间" v-model="model.custodyDate"  style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="充值金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recharge">
              <a-input-number :min="0" v-model="model.recharge" placeholder="请输入充值金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="充值方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargeType">
              <j-dict-select-tag type="list" v-model="model.rechargeType" dictCode="recharge_type" placeholder="请选择充值方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="充值日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargeDate">
              <j-date placeholder="请选择充值日期"  v-model="model.rechargeDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="代缴人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargeName">
              <a-input v-model="model.rechargeName" placeholder="请输入代缴人姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="与在押人员关系" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relation">
              <j-dict-select-tag type="list" v-model="model.relation" dictCode="relations" placeholder="请选择与在押人员关系" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="代缴人电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargePhone">
              <a-input v-model="model.rechargePhone" placeholder="请输入代缴人电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="代缴人身份证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rechargeIdnum">
              <a-input v-model="model.rechargeIdnum" placeholder="请输入代缴人身份证"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!--
          <a-col :span="8">
            <a-form-model-item label="证件信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificateInfo">
              <a-input v-model="model.certificateInfo" placeholder="请输入证件信息"  ></a-input>
            </a-form-model-item>
          </a-col>
          -->
          <a-col :span="8">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transactor">
              <a-input v-model="model.transactor" placeholder="请输入经办人" ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="8">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'dayjs'

  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'FpmsSksjForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{

        },
        confirmLoading: false,
        validatorRules: {
           billNum: [
              { required: true, message: '请输入票据单号!'},
           ],
           idNum: [
              { required: true, message: '请输入身份证号!'},
           ],
           balance: [
              { required: true, message: '请输入余额!'},
           ],
           recharge: [
              { required: true, message: '请输入充值金额!'},
           ],
        },
        url: {
          add: "/finance/fpmsSksj/add",
          edit: "/finance/fpmsSksj/edit",
          queryById: "/finance/fpmsSksj/queryById",
          rule: {
            billNum: ruleBaseURL + 'shop_order_num'
          },
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    // loaded  (){
    //   that.$nextTick(() => {
    //     that.form.setFieldsValue({"rechargeDate":sysDate});
    //     that.form.setFieldsValue({"transactor":sysUser});
    //   });
    // },
    methods: {
      add () {
        let record = this.modelDefault
        if(!record.billNum){
          this.getBillNum(record);
        }
        if(!record.transactor){
          record.transactor = this.$store.state.user.realname;
        }
        if(!record.rechargeType){
          record.rechargeType = "1";
        }
        if(!record.rechargeDate){
          record.rechargeDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
        }
        this.edit(record);
      },
      edit (record) {
        if(!record.billNum){
          this.getBillNum(record);
        }
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      getBillNum(record) {
        putAction(this.url.rule.billNum, this.model).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            record.billNum = res.result;
            this.model = Object.assign({}, record);
            this.visible = true;
          }
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.$router.push({ path: '/finance/FpmsSksj' });
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>